# 元组详解
print(type(("c.biancheng.net", 1, [2, 'a'], ("abc", 3.0))))

## python 创建元组
### 使用()直接创建
num = (7, 14, 21, 28, 35)
course = ("Python教程", "http://c.biancheng.net/python/")
abc = ("Python", 19, [1, 2], ('c', 2.0))

print("num =", num)
print("course =", course)
print("abc =", abc)

course = "Python教程", "http://c.biancheng.net/python/"
print("course =", course)

print()

# 最后加上逗号
a = ("http://c.bianchent.net/cplus/",)
print("type(a) =", type(a))
print("a =", a)

# 最后不加逗号
b = ("http://c.bianchent.net/socket/")
print("type(b) =", type(b))
print("b =", b)

print()
### 使用tuple()函数创建元组
# 将字符串转换成元组
tup1 = tuple("hello")
print("tup1 =", tup1)

# 将列表转换成元组
list1 = ["Python", "Java", "C++", "JavaScript"]
tup2 = tuple(list1)
print("tup2 =", tup2)

# 将字典转换成元组
dict1 = {"a": 100, "b": 42, "c": 9}
tup3 = tuple(dict1)
print("tup3 =", tup3)

# 将区间转换成元组
range1 = range(1, 6)
tup4 = tuple(range1)
print("tup4 =", tup4)

# 创建空元组
print("tuple() =", tuple())

print()

# Python访问元组元素
url = tuple("http://c.biancheng.net/shell/")
# 使用索引访问元组中的某个元素
print("url[3] =", url[3]) # 使用正数索引
print("url[-4] =", url[-4]) # 使用负数索引

# 使用切片访问元组中的一组元素
print("url[9: 18] =", url[9: 18]) # 使用正数切片
print("url[9: 18: 3]", url[9: 18: 3]) # 指定步长
print("url[-6: -1] =", url[-6: -1]) # 使用负数切片

print()

# Python修改元组
tup = (100, 0.5, -36, 73)
print("tup =", tup)
# 对元组进行重新赋值
tup = ("Shell脚本", "http://c.biancheng.net/shell/")
print("tup =", tup)

tup1 = (100, 0.5, -36, 73)
tup2 = (3 + 12j, -54.6, 99)
print("tup1 + tup2 =", tup1 + tup2)
print("tup1 =", tup1)
print("tup2 =", tup2)

print()
# Python删除元组
tup = ("Java教程", "http://c.bianchent.net/java/")
print("tup =", tup)
del tup
print("tup =", tup)
